<?php
namespace App\Api;
use Illuminate\Support\Facades\Response;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;


trait ApiResponse
{
    /**
     * http-code
     * @var int
     */
    protected $statusCode = FoundationResponse::HTTP_OK;

    /**
     * @return mixed
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    /**
     * 设置http-code
     * @param $statusCode
     * @return $this
     */
    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;
        return $this;
    }


    /**
     * @param $data
     * @param array $header
     * @return mixed
     */
    public function respond($data, $header = [])
    {

        return Response::json($data,$this->getStatusCode(),$header);
    }

    /**
     * @param $status
     * @param array $data
     * @param null $code
     * @return mixed
     */
    public function status($status, array $data, $code = null){

        if ($code){
            $this->setStatusCode($code);
        }
        $status = [
            'status' => $status,
            'code' => $this->statusCode
        ];

        $data = array_merge($status,$data);
        return $this->respond($data);

    }

    /**
     * 返回http-code等信息
     * @param string $message
     * @param int $code
     * @param string $status
     * @return mixed
     */
    public function failed(string $message, $code = FoundationResponse::HTTP_BAD_REQUEST,string $status = 'error'){

        return $this->setStatusCode($code)->message($message,$status);
    }


    /**
     * 以http-code 200返回json数据
     * @param $message
     * @param int $json_code
     * @param string $status
     * @return mixed
     */
    public function error($message,$json_code=400,$status = "error"){
        $return_data = [
            'status' => $status,
            'code' => $json_code,
            'message'=>$message,
        ];
        return $this->respond($return_data);
    }


    /**
     * http-code 200 正常返回数据
     * @param $data
     * @param string $status
     * @return mixed
     */
    public function success($data, $status = "success"){

        return $this->status($status,compact('data'));
    }


    /**
     * http-code 200 正常返回消息 不含数据
     * @param $message
     * @param string $status
     * @return mixed
     */
    public function message($message, $status = "success"){

        return $this->status($status,[
            'message' => $message
        ]);
    }

    /**
     * http-code 500 内部错误
     * @param string $message
     * @return mixed
     */
    public function internalError($message = "Internal Error!"){

        return $this->failed($message,FoundationResponse::HTTP_INTERNAL_SERVER_ERROR);
    }

    /**
     * http-code 401 未登录授权
     * @param string $message
     * @return mixed
     */
    public function unauthorized($message = "未登录或者口令已失效")
    {
        return $this->failed($message,FoundationResponse::HTTP_UNAUTHORIZED);

    }


    /**
     * http-code 403 登录但没有权限
     * @param string $message
     * @return mixed
     */
    public function forbidden($message = "没有权限")
    {
        return $this->failed($message,FoundationResponse::HTTP_FORBIDDEN);

    }


    /**
     * http-code 404 未找到资源
     * @param string $message
     * @return mixed
     */
    public function notFond($message = '没有此资源!')
    {
        return $this->failed($message,Foundationresponse::HTTP_NOT_FOUND);
    }

}
